package cn.myzf.fort.learn;

import java.util.Arrays;

/**
 * @Author: myzf
 * @date: 2021/8/25 10:50
 * @useful:
 * @modify: ===================================================
 * modifier            modifytime                description
 * ===================================================
 */
public class Lesson7_5 {
    public static void main(String[] args) {

        int[] insertNums = {18, 23,1, 6, 27, 15};

        insert(insertNums);

    }


    private static  void  insert(int[] nums){
        int i,j,k;
        for ( i = 1; i < nums.length; i++) {
            k = nums[i];
            j = i -1;
            while (j>=0 && k <nums[j]) {
                nums[j + 1] = nums[j];
                j--;
            }
            nums[j + 1] = k;
        }

    }




    /**
     * 插入排序
     */
    private static void insertSort(int[] nums) {
        int i, j, k;
        for (i = 1; i < nums.length; i++) {
            k = nums[i];
            j = i - 1;
            // 对 i 之前的数据，给当前元素找到合适的位置
            while (j >= 0 && k < nums[j]) {
                nums[j + 1] = nums[j];
                // j-- 继续往前寻找
                j--;
            }
            nums[j + 1] = k;
            System.out.print("第" + i + "次排序：");
            System.out.println(Arrays.toString(nums));
        }
    }

}
